﻿/*
 *author:keily ,department 2,wuda geo
 *create date:2013-1-29
 *description:图像操作类
 */

using System;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace GeoWeb.Common
{
    /// <summary>
    /// 图像操作类，包括读取图像文件，生成缩略图等
    /// </summary>
    public class ImageHelper
    {
        /// <summary>
        /// 打开文件并读取到数组中
        /// </summary>
        /// <param name="imagefilename">文件名包括完整路径</param>
        /// <returns>数组</returns>
        public byte[] ImageToBytes(string imagefilename)
        {
            FileStream stream = new FileStream(imagefilename, FileMode.OpenOrCreate);
            byte[] bytes = new byte[stream.Length];

            if (stream.CanRead)
            {
                stream.Read(bytes, 0, (int)stream.Length);
            }

            stream.Close();
            return bytes;
        }
        /// <summary>
        /// 将图像格式的二进制数组转换为 image对象
        /// </summary>
        /// <param name="content">图像格式的二进制数组</param>
        /// <returns>image对象</returns>
        public Image ConvertToImage(byte[] content)
        {
            MemoryStream ms = new MemoryStream(content);
            Image returnImage = Image.FromStream(ms);
            return returnImage;
        }


        /// <summary>
        /// 将image对象转换为图像格式的二进制数组
        /// </summary>
        /// <param name="image">Image对象</param>
        /// <returns>图像格式的二进制数组</returns>
        public byte[] ConvertToBytes(Image image)
        {
            MemoryStream ms = new MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            return ms.ToArray();
        }


        /// <summary>
        /// 将image对象转换为图像格式的二进制数组
        /// </summary>
        /// <param name="image">Image对象</param>
        /// <param name="format">图像的格式，ImageFormat类型</param>
        /// <returns>图像格式的二进制数组</returns>
        public byte[] ConvertToBytes(Image image, ImageFormat format)
        {
            MemoryStream ms = new MemoryStream();
            image.Save(ms, format);
            return ms.ToArray();
        }


        /// <summary>
        /// 将图像格式的二进制数组转换为缩略图二进制数组，默认格式为jpeg
        /// </summary>
        /// <param name="imageContents">源数组</param>
        /// <param name="thumbNailHeigth">缩略图Height</param>
        /// <param name="thumbNailWidth">缩略图Width</param>
        /// <returns>缩略图二进制数组</returns>
        public byte[] ConvertToThumbNail(byte[] imageContents, int thumbNailWidth, int thumbNailHeigth)
        {
            byte[] thumbnailBytes = null;
            //初始化缩略图高宽
            if (thumbNailWidth == 0) thumbNailWidth = 50;
            if (thumbNailHeigth == 0) thumbNailHeigth = 40;

            try
            {
                using (Image image = ConvertToImage(imageContents))
                {
                    // 生成缩略图对象
                    using (Image thumbnail = image.GetThumbnailImage(50, 40, null, new System.IntPtr()))
                    {
                        //将缩略图对象转换为数组
                        using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                        {
                            thumbnail.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                            thumbnailBytes = ms.ToArray();
                        }
                    }
                }
            }
            catch (Exception)
            {
                ;
            }
            return thumbnailBytes;
        }
        /// <summary>
        /// 将图像格式的二进制数组转换为缩略图二进制数组
        /// </summary>
        /// <param name="imageContents">源数组</param>
        /// <param name="thumbNailHeigth">缩略图Height</param>
        /// <param name="thumbNailWidth">缩略图Width</param>
        /// <param name="format">格式ImageFormat类型</param>
        /// <returns>image对象</returns>
        public bool ConvertToThumbNail(byte[] imageContents, int thumbNailWidth, int thumbNailHeigth, string filename,ImageFormat format)
        {
            bool blnThumbnailImage = false;
            //初始化缩略图高宽
            if (thumbNailWidth == 0) thumbNailWidth = 50;
            if (thumbNailHeigth == 0) thumbNailHeigth = 40;

            try
            {
                using (Image image = ConvertToImage(imageContents))
                {
                    // 生成缩略图对象
                    using (Image thumbnail = image.GetThumbnailImage(50, 40, null, new System.IntPtr()))
                    {
                        //将缩略图保存到指定文件中
                        thumbnail.Save(filename, format);
                        blnThumbnailImage = true;
                    }
                }
            }
            catch (Exception)
            {
                return false;
            }
            return blnThumbnailImage;
        }


        /// <summary>
        /// 返回图像的高宽，一个长度为2的数组，第一个为宽，第二个为高
        /// </summary>
        /// <param name="imageContents">图像数组</param>
        /// <returns>一个长度为2的数组，第一个为宽，第二个为高</returns>
        public int[] GetDimensions(byte[] imageContents)
        {
            Image image = ConvertToImage(imageContents);
            int width = Convert.ToInt32(image.PhysicalDimension.Width);
            int height = Convert.ToInt32(image.PhysicalDimension.Height);
            return new int[] { width, height } ;
        }
    }
}
